checkbox 控制元件的顯示


Posted by mijouhsieh on 2023-06-10

選取 checkbox 後第2個元件出現
React-input-checkbox-checked.png

1 . 第2個元件顯示與否設定成 state 變數: showB

const [showB, setShowB] = useState(true)

2 . input tag
checked屬性值代入 state 變數: showB
onChange屬性是 onChange handler,更換state為e.target.checked

<input 
  checked={showB}
  onChange={e => {
    setShowB(e.target.checked)
  }}
/>

3 . showB 為 true 時,就會顯示第2個 counter。

{showB && <Counter />}

e.target.checked

React-input-e.target.checked.png


React <input> - These <input> props are relevant both for uncontrolled and controlled inputs:

React-input-onChange-event.png

onChange:一個事件處理函式。

在受控輸入 controlled inputs 中是必需的。
當使用者更改輸入值時立即觸發(例如,對於每個按鍵輸入都會觸發)。
與瀏覽器的輸入事件相似。


#onchange #input #checkbox #checked







Related Posts

常用命令列指令

常用命令列指令

同步 & 非同步(5) - Promise.all 與 並行

同步 & 非同步(5) - Promise.all 與 並行

SQL-injection專論 (4) -- Blind SQL injection (2)

SQL-injection專論 (4) -- Blind SQL injection (2)


Comments